﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using MiniFox.Web.Configurations;

namespace MiniFox.Web.AppStarting
{
    /// <summary>
    /// 
    /// </summary>
    internal class RegisterRoutes : HttpStartBase
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="hostEnvironment"></param>
        /// <param name="webConfiguration"></param>
        public RegisterRoutes(IConfiguration configuration, IWebHostEnvironment hostEnvironment, WebConfiguration webConfiguration) : base(configuration, hostEnvironment, webConfiguration)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="app"></param>
        public override void OnApplicationConfiguring(IApplicationBuilder app)
        {
            app.UseEndpoints(endpoints =>
            {
                foreach (var routeSetting in this.WebConfiguration.RouteSettings)
                {
                    string urlTemplate = routeSetting.UrlTemplate.TrimStart('/');

                    endpoints.MapControllerRoute(
                        name: routeSetting.Name,
                        pattern: urlTemplate,
                        defaults: routeSetting.Parameters,
                        constraints: routeSetting.Constraints
                    );
                }
            });
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="services"></param>
        public override void OnServicesConfiguring(IServiceCollection services)
        {
        }
    }
}
